package com.penghaisoft.microwms.material.web.outwarehouse.service.impl;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.data.model.RawHandOut;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.BaseService;
import com.penghaisoft.microwms.common.wms.model.ProHandIn;
import com.penghaisoft.microwms.material.web.outwarehouse.dao.RawHandOutMapper;
import com.penghaisoft.microwms.material.web.outwarehouse.service.RawHandOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * Created by 王治国 on 2018/5/25.
 */
@Service
public class RawHandOutServiceImpl extends BaseService implements RawHandOutService {

    @Autowired
    private RawHandOutMapper rawHandOutMapper;

    @Override
    public Pager<RawHandOut> findAll(int page, int rows, RawHandOut rawHandOut) {
        long size = rawHandOutMapper.queryCount(rawHandOut);
        Pager<RawHandOut> pager = new Pager<>();
        List<RawHandOut> result = new ArrayList<>();
        pager.setPage(page);
        pager.setRows(rows);
        if (size > 0) {
            result = rawHandOutMapper.queryList(pager, rawHandOut);
        }
        pager.setRecords(result);
        pager.setTotalCount(size);
        return pager;
    }

    @Override
    public List<RawHandOut> findAllExcel(RawHandOut rawHandOut) {
        return null;
    }

    @Override
    public Resp createOutOrder(RawHandOut rawHandOut) {
        Resp resp = new Resp();
//        rawHandOut.setHandOutId(CommonUtils.getUUID());
        int count = rawHandOutMapper.createOutOrder(rawHandOut);
        if (count == 1) {

            resp.setCode(String.valueOf(count));
        } else {
            resp.setCode(Constant.FAIL);
        }
        return resp;
    }

	/* @Title: batchInsert 
	 * @Description: 批量创建手工出库任务
	 * @author jzh
	 * @date 2018年6月19日 下午3:59:46 
	 */
    @Transactional
	@Override
	public Resp batchInsert(List<RawHandOut> list, String handOutNo, String loginName) {
		Date now = new Date();
		HashSet<String> allSet = new HashSet<String>();
    	for(int i = 0 ; i < list.size(); i ++){
    		RawHandOut tmpInsertOb = list.get(i);
    		
    		StringBuffer uniqueStr = new StringBuffer("");
    		uniqueStr.append(tmpInsertOb.getModel());
    		uniqueStr.append(tmpInsertOb.getModelType());
    		if(allSet.contains(uniqueStr.toString())){
    			Resp resp = new Resp();
    			resp.setCode(Constant.FAIL);
    			resp.setMsg("选择的型号、型号类型重复");
    			return resp;
    		}else{
    			allSet.add(uniqueStr.toString());
    		}
    	}
    	
    	for(int i = 0 ; i < list.size(); i ++){
    		RawHandOut tmpInsertOb = list.get(i);
    		
//    		合同编号、型号、型号类型、需求数量 页面传递的参数已经提供
    		tmpInsertOb.setHandOutNo(handOutNo);
//    		0新建 1入库中 2入库完成
    		tmpInsertOb.setHandOutStatus("0");
    		tmpInsertOb.setActiveFlag("1");
    		tmpInsertOb.setGmtCreate(now);
    		tmpInsertOb.setCreateBy(loginName);
    		rawHandOutMapper.create(tmpInsertOb);
    	}
    	
		return success();
	}
}
